using UnityEngine;

public class FreeCameraController : MonoBehaviour
{
    [Header("移动参数")]
    public float moveSpeed = 5f;
    public float sprintMultiplier = 3f;
    public float verticalSpeed = 2f;

    [Header("旋转参数")] 
    public float rotationSpeed = 2f;
    public float minVerticalAngle = -89f;
    public float maxVerticalAngle = 89f;

    private float rotationX;
    private Vector3 moveDirection;

    void Update()
    {
        // 键盘移动控制
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        moveDirection = transform.right * horizontal + transform.forward * vertical;
        
        // 加速移动
        float currentSpeed = moveSpeed * (Input.GetKey(KeyCode.LeftShift) ? sprintMultiplier : 1f);
        transform.position += moveDirection * currentSpeed * Time.deltaTime;

        // 垂直移动
        if(Input.GetKey(KeyCode.Q))
            transform.position += Vector3.up * verticalSpeed * Time.deltaTime;
        else if(Input.GetKey(KeyCode.E))
            transform.position -= Vector3.up * verticalSpeed * Time.deltaTime;

        // 鼠标右键旋转控制
        if(Input.GetMouseButton(1))
        {
            float mouseX = Input.GetAxis("Mouse X") * rotationSpeed;
            float mouseY = Input.GetAxis("Mouse Y") * rotationSpeed;

            rotationX -= mouseY;
            rotationX = Mathf.Clamp(rotationX, minVerticalAngle, maxVerticalAngle);

            transform.localRotation = Quaternion.Euler(rotationX, transform.localEulerAngles.y, 0f);
            transform.Rotate(Vector3.up * mouseX, Space.World);
        }
    }
}